package org.redr.widgetGUI.core;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.redr.widgetGUI.settings.WidgetSettings;
import org.redr.widgetGUI.settings.WidgetState;
import org.redr.widgetGUI.settings.XMLSetting;
import org.redr.widgetGUI.settings.settingsTools.ItemsHandler;

import com.trolltech.qt.gui.QBoxLayout;
import com.trolltech.qt.gui.QButtonGroup;
import com.trolltech.qt.gui.QCheckBox;
import com.trolltech.qt.gui.QGroupBox;
import com.trolltech.qt.gui.QVBoxLayout;
import com.trolltech.qt.gui.QWidget;

public class CheckBox extends WidgetState{
	public QButtonGroup buttonGroup;
	public QGroupBox	buttonBox;
	Map<String, QCheckBox> buttonMapping = new HashMap<String, QCheckBox>();
	
	public QWidget widget(){
		return widgetBox;
	}
	
	public CheckBox(LayoutState parent, WidgetSettings ws) {
		super(parent, ws);
		buttonBox	=	new QGroupBox(_getSetting("label", "!!FIX ME LABEL SHOULD BE SET!!"));
		buttonBox.setLayout(new QVBoxLayout());
		buttonGroup = new QButtonGroup();
		buttonGroup.setExclusive(false);
//		addWidget(buttonBox);
		_loadSettings();
	}

	private void _loadSettings() {
		// TODO Auto-generated method stub
		ItemsHandler buttonText = settings.items("buttons");
		ItemsHandler buttonChecked = settings.items("checked");
		ItemsHandler buttonToolTip = settings.items("toolTip");
		System.out.println(XMLSetting.xmlToString(settings.e));
		for (String key : buttonText.getKeys()){
			String text = buttonText.getValue(key);
			String toolTip = null;
			if (buttonToolTip.containsItem(key)){
				toolTip = buttonToolTip.getValue(key);
			}
			QCheckBox cb = addButton(text, toolTip, buttonChecked.containsItem(key));
			
			cb.stateChanged.connect(this, "_selectionsChanged(Integer)");
			buttonBox.layout().addWidget(cb);
			buttonGroup.addButton(cb);
			buttonMapping.put(key, cb);
		}
		
	}
	
	public QCheckBox addButton(String text, String toolTip, boolean checked){
		QCheckBox cb;
		if (text != null) cb= new QCheckBox(text);
		else cb = new QCheckBox();
		if (toolTip != null) cb.setToolTip(toolTip);
		cb.setChecked(checked);
		
		return cb;
	}
	
	public void clear(){
		
	}
	
	
	
	/**
	 * Called when a button is clicked.  We clear the list of checked buttons in the settings and add only the clicked boxes.
	 * 
	 * @param i
	 */
	public void _selectionsChanged(Integer i){
		ItemsHandler buttonChecked = settings.items("checked");
		buttonChecked.clear();
		for (String key : buttonMapping.keySet()){
			if (buttonMapping.get(key).isChecked()) buttonChecked.addItem(key, "checked");
		}
	}
	
	public List<String> checkedButtons(){
		List<String> result = new Vector<String>();
		for (String key : buttonMapping.keySet()){
			if (buttonMapping.get(key).isChecked()){
				result.add(key);
			}
		}
		return result;
	}
	
	public void setCheckedButtons(List<String> checkedButtons){
		for(String key : checkedButtons){
			if (buttonMapping.containsKey(key)) buttonMapping.get(key).setChecked(true);
		}
	}

}
